package com.example.grape_utils.image;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.example.grape_utils.MediaController;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtils {

    public static Bitmap rotateBitmap(Bitmap bitmap, float rotateDegree) {
        Matrix matrix = new Matrix(); // 创建操作图片用的矩阵对象
        matrix.postRotate(rotateDegree); // 执行图片的旋转动作
        // 创建并返回旋转后的位图对象
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, false);
    }

    public static void saveBitmapToGallery(Bitmap bitmap, String path, Context context) {
        File file = new File(path);
        saveBitmap(bitmap, null, file);
        try {
            // 下面会创造一个缩略图，导致图库里面有2个一样的图片，一个原图，一个缩略图
//            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null);
            //简单实现
//            ContentValues values = new ContentValues();
//            values.put(MediaStore.Images.Media.DATA, mFile.getAbsolutePath());
//            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
//            Uri uri =context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//            context.sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(mFile)));

            MediaController.insertImage(context, file.getName(), System.currentTimeMillis(), 0, bitmap.getWidth() * bitmap.getHeight() * 4, file.getAbsolutePath(), bitmap.getWidth(), bitmap.getHeight(),null);
            context.sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(file)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //支持保存图片、图片bytes，位置可以是:地址或者文件
    public static void saveBitmap(Bitmap bitmap, String path, File file) {
        if (path == null && file == null) {
            Log.e("BitmapUtils", " saveBitmap path and file must has one not null");
        }
        FileOutputStream output = null;
        try {
            if (file == null) {
                file = new File(path);
            }
            output = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
            output.flush();
            output.close();
        } catch (Exception e) {
            Log.e("BitmapUtils", " saveBitmap error " + e.getMessage());
        }
    }

    public static void saveBitmap(byte[] bytes, String path, File file) {
        if (path == null && file == null) {
            Log.e("BitmapUtils", " saveBitmap path and file must has one not null");
        }
        if (file == null) {
            file = new File(path);
        }
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(file);
            output.write(bytes);
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
        byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
        // Rotate the Y luma
        int i = 0;
        for (int x = 0; x < imageWidth; x++) {
            for (int y = imageHeight - 1; y >= 0; y--) {
                yuv[i] = data[y * imageWidth + x];
                i++;
            }
        }
        // Rotate the U and V color components
        i = imageWidth * imageHeight * 3 / 2 - 1;
        for (int x = imageWidth - 1; x > 0; x = x - 2) {
            for (int y = 0; y < imageHeight / 2; y++) {
                yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
                i--;
                yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
                i--;
            }
        }
        return yuv;
    }

    public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
    }


}
